package com.freesun.sys.controller;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freesun.sys.common.DataGridView;
import com.freesun.sys.common.ResultObj;
import com.freesun.sys.domain.Logininfo;
import com.freesun.sys.service.LogininfoService;
import com.freesun.sys.vo.LogininfoVo;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 郑枫
 * @since 2019-10-17
 */
@RestController
@RequestMapping("/loginfo")
public class LogininfoController {

	//buzh
	@Autowired
	private LogininfoService logininfoService;
	private Object loginfoService;
	
	@RequestMapping("loadAllLoginfo")
	public DataGridView loadAllLoginfo(LogininfoVo logininfoVo) {
		
		
		IPage<Logininfo> page=new Page<>(logininfoVo.getPage(),logininfoVo.getLimit());
		QueryWrapper<Logininfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(logininfoVo.getLoginname()), "loginname",logininfoVo.getLoginname());
		queryWrapper.like(StringUtils.isNotBlank(logininfoVo.getLoginip()), "loginip",logininfoVo.getLoginip());
		queryWrapper.ge(logininfoVo.getStartTime()!=null, "logintime",logininfoVo.getStartTime());
		queryWrapper.le(logininfoVo.getEndTime()!=null, "logintime",logininfoVo.getEndTime());
		queryWrapper.orderByDesc("logintime");
		this.logininfoService.page(page, queryWrapper);
		
		return new DataGridView(page.getTotal(),page.getRecords());
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("deleteLoginfo")
	public ResultObj deleteLoginfo(Integer id) {
		try {
			this.logininfoService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
	
	/**
	 * 批量删除
	 */
	@RequestMapping("batchDeleteLoginfo")
	public ResultObj batchDeleteLoginfo(LogininfoVo loginfoVo) {
		try {
			Collection<Serializable> idList=new ArrayList<Serializable>();
			for (Integer id : loginfoVo.getIds()) {
				idList.add(id);
			}
			this.logininfoService.removeByIds(idList);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
}
